<?php
	namespace App\Controller;
	use \Psr\Http\Message\ServerRequestInterface as Request;
    use \Psr\Http\Message\ResponseInterface as Response;
    use App\Model\UserModel;
    use stdClass;
	class RolrController extends BaseController{
       public function RolrList($request, $response, $args){
       	include APP_PATH.'/View/Rolr/rolrlist.html';
       }
        public function PostRolrList($request, $response, $args){
         	$sql="select * from rolr";
        	$draw=$request->getParam('draw')*1;//第几次请求
			$start=$request->getParam('start');//第一条数据开始的索引
			$length=$request->getParam('length');//返回的数据长度
		    $col=$_GET['order'][0]['column'];//那一列排序
			$dir=$_GET['order'][0]['dir'];//升序还是降序
		    $orderSql = "";
		    if(isset($col)){
			    $i = intval($col);
			    switch($i){
			        case 2;$orderSql = " order by nickname ".$dir;break;
			        case 3;$orderSql = " order by email ".$dir;break;
			        case 4;$orderSql = " order by phone ".$dir;break;
			        default;$orderSql = '';
			    }
			}
			$sql=$sql.$orderSql;
	     	$user=new UserModel();
      	    $ret=$user->queryAll($sql);
		    $recordsTotal=count($ret);
		    $recordsFiltered = $recordsTotal;
		    $data=array();
		    $h=0;
		    for($i=$start;$i<$start+$length;$i++,$h++){
		    	if(!empty($ret[$i])){
		    		$data[$h]=$ret[$i];
		    	}else{
		    		break;
		    	}
		    }
		    for($i=0,$index=$start+1;$i<count($data);$i++,$index++){
		    	$data[$i]['ID']=$index;
		    }
		    echo json_encode([
		    "draw" => $draw,
		    "recordsFiltered" =>$recordsFiltered,
		    "recordsTotal" =>$recordsTotal,
		    'data'=>$data
		    ]);
       }
       public function RolrItem($request, $response, $args){
       	    $RolrID=$request->getParam('RolrID');
       	    $draw=$request->getParam('draw')*1;//第几次请求
			$start=$request->getParam('start');//第一条数据开始的索引
			$length=$request->getParam('length');//返回的数据长度
		    $col=$_GET['order'][0]['column'];//那一列排序
			$dir=$_GET['order'][0]['dir'];//升序还是降序
			$sql="select ID,NickName,Email,Phone,Sex from user where RolrID=".$RolrID."";
		    $orderSql = "";
		    if(isset($col)){
			    $i = intval($col);
			    switch($i){
			        case 0;$orderSql = " order by id ".$dir;break;
			        case 1;$orderSql = " order by rolrname ".$dir;break;
			        default;$orderSql = '';
			    }
			}
			$sql=$sql.$orderSql;
	     	$user=new UserModel();
      	    $ret=$user->queryAll($sql);
		    $recordsTotal=count($ret);
		    $recordsFiltered = $recordsTotal;
		    $data=array();
		    $h=0;
		    for($i=$start;$i<$start+$length;$i++,$h++){
		    	if(!empty($ret[$i])){
		    		$data[$h]=$ret[$i];
		    	}else{
		    		break;
		    	}
		    }
		    echo json_encode([
		    "draw" => $draw,
		    "recordsFiltered" =>$recordsFiltered,
		    "recordsTotal" =>$recordsTotal,
		    'data'=>$data
		    ]);
        }
        public function PostRolrItem($request, $response, $args){
       	    $RolrID=$request->getParam('RolrID');
			$sql="select ID,NickName,Email,Phone,Sex from user where RolrID=".$RolrID."";
			$user=new UserModel();
			$ret=$user->query($sql);
		    echo json_encode([
		       'data'=>$ret
		    ]);
        }
        public function RolrDelete($request, $response, $args){
       	    $RolrName=$request->getParam('RolrName');
			$sql="delete from rolr where rolrname='".$RolrName."'";
			$user=new UserModel();
			$ret=$user->query($sql);
		    echo json_encode([
		       'data'=>$ret
		    ]);
        }
         public function PowerItem($request, $response, $args){
       	   $PowerName=$request->getParam('PowerName');
       	    $user=new UserModel();
			$sql="select power,powername from rolr
					inner join rolrpower
					on rolr.id=rolrpower.rolrid
					inner join power
					on rolrpower.power=power.id
					where rolrname='".$PowerName."'";
			$draw=$request->getParam('draw')*1;//第几次请求
			$start=$request->getParam('start');//第一条数据开始的索引
			$length=$request->getParam('length');//返回的数据长度
		    $col=$_GET['order'][0]['column'];//那一列排序
			$dir=$_GET['order'][0]['dir'];//升序还是降序
		    $orderSql = "";
		    if(isset($col)){
			    $i = intval($col);
			    switch($i){
			        case 2;$orderSql = " order by rolrname ".$dir;break;
			        default;$orderSql = '';
			    }
			}
			$sql=$sql.$orderSql;
	     	$user=new UserModel();
      	    $ret=$user->queryAll($sql);
		    $recordsTotal=count($ret);
		    $recordsFiltered = $recordsTotal;
		    $data=array();
		    $h=0;
		    for($i=$start;$i<$start+$length;$i++,$h++){
		    	if(!empty($ret[$i])){
		    		$data[$h]=$ret[$i];
		    	}else{
		    		break;
		    	}
		    }
		    echo json_encode([
		    "draw" => $draw,
		    "recordsFiltered" =>$recordsFiltered,
		    "recordsTotal" =>$recordsTotal,
		    'data'=>$data
		    ]);
        }
        public function PowerAllItem($request, $response, $args){
       	    $user=new UserModel();
			$sql="select powername from power";
      	    $ret=$user->queryAll($sql);
		    echo json_encode([
		    'data'=>$ret
		    ]);
        }
         public function PowerId($request, $response, $args){
           	$PowerName=$request->getParam('PowerName');
         	$user=new UserModel();
			$sql="select id from power where powername='".$PowerName."'";
            $ret=$user->queryAll($sql);
		    echo json_encode([
		    'data'=>$ret
		    ]);
        }

        public function RolrSelect($request, $response, $args){
          $Name=$request->getParam('NickName');
          $Email=$request->getParam('Email');
          $user=new UserModel();
          $res=$user->Rolrselect($Name,$Email);
          if(empty($res)){
          	 echo json_encode([
               'code'=>1
            ]);
          }else{
          	 echo json_encode([
               'code'=>$res
            ]);
          }
        }
         public function RolrAdd($request, $response, $args){
          $RolrName=$request->getParam('RolrName');
          $data=array();
          $data['RolrName']=$RolrName;
          $user=new UserModel();
          $res=$user->addrolr('rolr',$data);
          echo json_encode([
             'code'=>$res
          ]);
        }
        public function PowerAdd($request, $response, $args){
          $RolrId=$request->getParam('RolrId');
          $Power=$request->getParam('Power');
          $data=array();
          $data['RolrID']=$RolrId;
          $data['Power']=$Power;
          $user=new UserModel();
          $ret=$user->addpower("rolrpower",$data);
          echo json_encode([
          	'code'=>$ret
          ]);
        }
        public function PowerList($request, $response, $args){
          include APP_PATH.'/View/Rolr/powerlist.html';
        }
	}